/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.gui.mvc.controller.testbed;

import org.testis.gui.mvc.controller.AbstractController;
import org.testis.gui.mvc.model.testbed.TbedPopupModel;
import org.testis.gui.mvc.view.testbed.TbedPopupMenu;
import org.testis.orm.testbed.refined.DeviceDefinitionRefined;
import org.testis.orm.testbed.refined.DeviceFirmwareRefined;
import org.testis.orm.testbed.refined.DeviceGroupDetailRefined;
import org.testis.orm.testbed.refined.DeviceGroupRefined;
import org.testis.orm.testbed.refined.DirectSerialConfigRefined;
import org.testis.orm.testbed.refined.DutRefined;
import org.testis.orm.testbed.refined.NetworkAdapterRefined;
import org.testis.orm.testbed.refined.NetworkSerialConfigRefined;
import org.testis.orm.testbed.refined.SerialConnectionRefined;
import org.testis.orm.testbed.refined.TestPcRefined;
import org.testis.utils.tree.TbedMutableTreeNode;
import java.awt.event.MouseEvent;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author selcuk
 */
public class TbedPopupController extends AbstractController {

    public static final String OPEN_POPUP_MENU = "OpenMenu";
    public static final String POPUP_MENU_ADD_DEVICE_VISIBILITY = "AddNewDeviceVisibility";
    public static final String POPUP_MENU_EDIT_DEVICE_VISIBILITY = "EditDeviceVisibility";
    public static final String POPUP_MENU_DELETE_DEVICE_VISIBILITY = "DeleteDeviceVisibility";
    public static final String POPUP_MENU_ADD_FW_VISIBILITY = "AddNewFirmwareVisibility";
    public static final String POPUP_MENU_EDIT_FW_VISIBILITY = "EditFirmwareVisibility";
    public static final String POPUP_MENU_DELETE_FW_VISIBILITY = "DeleteFirmwareVisibility";
    public static final String POPUP_MENU_ADD_TESTPC_VISIBILITY = "AddNewTestPcVisibility";
    public static final String POPUP_MENU_EDIT_TESTPC_VISIBILITY = "EditTestPcVisibility";
    public static final String POPUP_MENU_DELETE_TESTPC_VISIBILITY = "DeleteTestPcVisibility";
    public static final String POPUP_MENU_ADD_DEVICEGROUP_VISIBILITY = "AddNewDeviceGroupVisibility";
    public static final String POPUP_MENU_EDIT_DEVICEGROUP_VISIBILITY = "EditDeviceGroupVisibility";
    public static final String POPUP_MENU_DELETE_DEVICEGROUP_VISIBILITY = "DeleteDeviceGroupVisibility";
    public static final String POPUP_MENU_ADD_DEVICEGROUPDETAIL_VISIBILITY = "AddDeviceGroupDetailVisibility";
    public static final String POPUP_MENU_EDIT_DEVICEGROUPDETAIL_VISIBILITY = "EditDeviceGroupDetailVisibility";
    public static final String POPUP_MENU_DELETE_DEVICEGROUPDETAIL_VISIBILITY = "DeleteDeviceGroupDetailVisibility";
    public static final String POPUP_MENU_ADD_DUT_VISIBILITY = "AddNewDutVisibility";
    public static final String POPUP_MENU_EDIT_DUT_VISIBILITY = "EditDutVisibility";
    public static final String POPUP_MENU_DELETE_DUT_VISIBILITY = "DeleteDutVisibility";
    public static final String POPUP_MENU_ADD_NETWORKADAPTER_VISIBILITY = "AddNewNetworkAdapterVisibility";
    public static final String POPUP_MENU_EDIT_NETWORKADAPTER_VISIBILITY = "EditNetworkAdapterVisibility";
    public static final String POPUP_MENU_DELETE_NETWORKADAPTER_VISIBILITY = "DeleteNetworkAdapterVisibility";
    public static final String POPUP_MENU_ADD_SERIAL_VISIBILITY = "AddSerialVisibility";
    public static final String POPUP_MENU_EDIT_SERIAL_VISIBILITY = "EditSerialVisibility";
    public static final String POPUP_MENU_DELETE_SERIAL_VISIBILITY = "DeleteSerialVisibility";
    public static final String POPUP_MENU_ADD_DIRECTSERIAL_VISIBILITY = "AddDirectSerialVisibility";
    public static final String POPUP_MENU_EDIT_DIRECTSERIAL_VISIBILITY = "EditDirectSerialVisibility";
    public static final String POPUP_MENU_DELETE_DIRECTSERIAL_VISIBILITY = "DeleteDirectSerialVisibility";
    public static final String POPUP_MENU_ADD_NETWORKSERIAL_VISIBILITY = "AddNetworkSerialVisibility";
    public static final String POPUP_MENU_EDIT_NETWORKSERIAL_VISIBILITY = "EditNetworkSerialVisibility";
    public static final String POPUP_MENU_DELETE_NETWORKSERIAL_VISIBILITY = "DeleteNetworkSerialVisibility";
    private LinkedList<String> m_visibilityPropertyList = new LinkedList<String>();

    public TbedPopupController() {
        super();
        initialize();
    }

    public void operationOpenPopupMenu(TbedMutableTreeNode node, MouseEvent evt) {
        if (node.getUserObject() != null) {
            String typeName = node.getTypeName();
            if (typeName.equals(DeviceDefinitionRefined.TYPE_NAME)) {
                String[] arr = {POPUP_MENU_EDIT_DEVICE_VISIBILITY,
                    POPUP_MENU_DELETE_DEVICE_VISIBILITY,
                    POPUP_MENU_ADD_FW_VISIBILITY,};
                setMenuItemVisibilities(arr);
            } else if (typeName.equals(DeviceFirmwareRefined.TYPE_NAME)) {
                String[] arr = {POPUP_MENU_EDIT_FW_VISIBILITY,
                    POPUP_MENU_DELETE_FW_VISIBILITY};
                setMenuItemVisibilities(arr);
            } else if (typeName.equals(DeviceGroupDetailRefined.TYPE_NAME)) {
                String[] arr = {POPUP_MENU_EDIT_DEVICEGROUPDETAIL_VISIBILITY,
                    POPUP_MENU_DELETE_DEVICEGROUPDETAIL_VISIBILITY};
                setMenuItemVisibilities(arr);
            } else if (typeName.equals(DeviceGroupRefined.TYPE_NAME)) {
                String[] arr = {POPUP_MENU_EDIT_DEVICEGROUP_VISIBILITY,
                    POPUP_MENU_DELETE_DEVICEGROUP_VISIBILITY,
                    POPUP_MENU_ADD_DEVICEGROUPDETAIL_VISIBILITY};
                setMenuItemVisibilities(arr);
            } else if (typeName.equals(TestPcRefined.TYPE_NAME)) {
                String[] arr = {POPUP_MENU_EDIT_TESTPC_VISIBILITY,
                    POPUP_MENU_DELETE_TESTPC_VISIBILITY,
                    POPUP_MENU_ADD_DUT_VISIBILITY,
                    POPUP_MENU_ADD_NETWORKADAPTER_VISIBILITY};
                setMenuItemVisibilities(arr);
            } else if (typeName.equals(DutRefined.TYPE_NAME)) {
                String[] arr = {POPUP_MENU_EDIT_DUT_VISIBILITY,
                    POPUP_MENU_DELETE_DUT_VISIBILITY};
                setMenuItemVisibilities(arr);
            } else if (typeName.equals(NetworkAdapterRefined.TYPE_NAME)) {
                String[] arr = {POPUP_MENU_EDIT_NETWORKADAPTER_VISIBILITY,
                    POPUP_MENU_DELETE_NETWORKADAPTER_VISIBILITY};
                setMenuItemVisibilities(arr);
            } else if (typeName.equals(SerialConnectionRefined.TYPE_NAME)) {
                String[] arr = {POPUP_MENU_EDIT_SERIAL_VISIBILITY,
                    POPUP_MENU_DELETE_SERIAL_VISIBILITY};
                setMenuItemVisibilities(arr);
            } else if (typeName.equals(DirectSerialConfigRefined.TYPE_NAME)) {
                String[] arr = {POPUP_MENU_EDIT_DIRECTSERIAL_VISIBILITY,
                    POPUP_MENU_DELETE_DIRECTSERIAL_VISIBILITY, POPUP_MENU_ADD_SERIAL_VISIBILITY};
                setMenuItemVisibilities(arr);
            } else if (typeName.equals(NetworkSerialConfigRefined.TYPE_NAME)) {
                String[] arr = {POPUP_MENU_EDIT_NETWORKSERIAL_VISIBILITY,
                    POPUP_MENU_DELETE_NETWORKSERIAL_VISIBILITY, POPUP_MENU_ADD_SERIAL_VISIBILITY};
                setMenuItemVisibilities(arr);
            } else if (typeName.equals("String")) {
                String strRootNode = (String) node.getUserObject();
                if (strRootNode.equals(TbedTreeController.DEVICES_NODE_NAME)) {
                    String[] arr = {POPUP_MENU_ADD_DEVICE_VISIBILITY};
                    setMenuItemVisibilities(arr);
                } else if (strRootNode.equals(
                        TbedTreeController.DEVICE_GROUP_NODE_NAME)) {
                    String[] arr = {POPUP_MENU_ADD_DEVICEGROUP_VISIBILITY};
                    setMenuItemVisibilities(arr);
                } else if (strRootNode.equals(
                        TbedTreeController.TEST_PC_NODE_NAME)) {
                    String[] arr = {POPUP_MENU_ADD_TESTPC_VISIBILITY};
                    setMenuItemVisibilities(arr);
                } else if (strRootNode.equals(
                        TbedTreeController.SERIAL_CONN_NODE_NAME)) {
                    String[] arr = {POPUP_MENU_ADD_DIRECTSERIAL_VISIBILITY,
                        POPUP_MENU_ADD_NETWORKSERIAL_VISIBILITY};
                    setMenuItemVisibilities(arr);
                } else {
                    String[] arr = {};
                    setMenuItemVisibilities(arr);
                }
            } else {
                String[] arr = {};
                setMenuItemVisibilities(arr);
            }
            setModelProperty(OPEN_POPUP_MENU, evt);
        }

    }

    private void initialize() {
        this.addView(new TbedPopupMenu());
        this.addModel(new TbedPopupModel());
        initVisibilityList();
    }

    private void initVisibilityList() {
        m_visibilityPropertyList.add(POPUP_MENU_ADD_DEVICE_VISIBILITY);
        m_visibilityPropertyList.add(POPUP_MENU_EDIT_DEVICE_VISIBILITY);
        m_visibilityPropertyList.add(POPUP_MENU_DELETE_DEVICE_VISIBILITY);
        m_visibilityPropertyList.add(POPUP_MENU_ADD_FW_VISIBILITY);
        m_visibilityPropertyList.add(POPUP_MENU_EDIT_FW_VISIBILITY);
        m_visibilityPropertyList.add(POPUP_MENU_DELETE_FW_VISIBILITY);
        m_visibilityPropertyList.add(POPUP_MENU_ADD_TESTPC_VISIBILITY);
        m_visibilityPropertyList.add(POPUP_MENU_EDIT_TESTPC_VISIBILITY);
        m_visibilityPropertyList.add(POPUP_MENU_DELETE_TESTPC_VISIBILITY);
        m_visibilityPropertyList.add(POPUP_MENU_ADD_DEVICEGROUP_VISIBILITY);
        m_visibilityPropertyList.add(POPUP_MENU_EDIT_DEVICEGROUP_VISIBILITY);
        m_visibilityPropertyList.add(POPUP_MENU_DELETE_DEVICEGROUP_VISIBILITY);
        m_visibilityPropertyList.add(POPUP_MENU_ADD_DEVICEGROUPDETAIL_VISIBILITY);
        m_visibilityPropertyList.add(
                POPUP_MENU_EDIT_DEVICEGROUPDETAIL_VISIBILITY);
        m_visibilityPropertyList.add(
                POPUP_MENU_DELETE_DEVICEGROUPDETAIL_VISIBILITY);
        m_visibilityPropertyList.add(POPUP_MENU_ADD_DUT_VISIBILITY);
        m_visibilityPropertyList.add(POPUP_MENU_EDIT_DUT_VISIBILITY);
        m_visibilityPropertyList.add(POPUP_MENU_DELETE_DUT_VISIBILITY);
        m_visibilityPropertyList.add(POPUP_MENU_ADD_NETWORKADAPTER_VISIBILITY);
        m_visibilityPropertyList.add(POPUP_MENU_EDIT_NETWORKADAPTER_VISIBILITY);
        m_visibilityPropertyList.add(POPUP_MENU_DELETE_NETWORKADAPTER_VISIBILITY);
        m_visibilityPropertyList.add(POPUP_MENU_ADD_SERIAL_VISIBILITY);
        m_visibilityPropertyList.add(POPUP_MENU_EDIT_SERIAL_VISIBILITY);
        m_visibilityPropertyList.add(POPUP_MENU_DELETE_SERIAL_VISIBILITY);
        m_visibilityPropertyList.add(POPUP_MENU_ADD_DIRECTSERIAL_VISIBILITY);
        m_visibilityPropertyList.add(POPUP_MENU_EDIT_DIRECTSERIAL_VISIBILITY);
        m_visibilityPropertyList.add(POPUP_MENU_DELETE_DIRECTSERIAL_VISIBILITY);
        m_visibilityPropertyList.add(POPUP_MENU_ADD_NETWORKSERIAL_VISIBILITY);
        m_visibilityPropertyList.add(POPUP_MENU_EDIT_NETWORKSERIAL_VISIBILITY);
        m_visibilityPropertyList.add(POPUP_MENU_DELETE_NETWORKSERIAL_VISIBILITY);
    }

    private void setMenuItemVisibilities(String[] visibles) {

        List _temp = Arrays.asList(visibles);
        for (Iterator<String> it = m_visibilityPropertyList.iterator(); it.hasNext();) {
            String property = (String) it.next();
            if (_temp.contains(property)) {
                setModelProperty(property, true);
            } else {
                setModelProperty(property, false);
            }
        }
    }
}
